package com.example.shareapp.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.example.shareapp.R;
import com.example.shareapp.api.UriApi;
import com.example.shareapp.bean.AllDataEntry;
import com.example.shareapp.utils.DensityUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cx on 2017/4/7.
 */

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.ViewHolder> {
    private LayoutInflater inflater;
    private Context context;
    private List<AllDataEntry.ARuanjianLiebiaos> list;
    private ArrayList<String> imgList;
    int d_20;
    int d_5;

    public ListAdapter(Context context,List<AllDataEntry.ARuanjianLiebiaos> list) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        d_20 = DensityUtils.dip2px(context,20);
        d_5 = DensityUtils.dip2px(context,5);
        this.list = list;
    }

    private OnClick onItemClickListener;

    public void setOnItemClickListener(OnClick onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }




    public class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(View arg0) {
            super(arg0);
        }
        public ImageView iv_icon;
        public TextView tv_name;
        public TextView tv_content;
        public TextView tv_index;
        public Button btn_xz;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.list_item, null);
        ViewHolder vh = new ViewHolder(view);
        vh.iv_icon = view.findViewById(R.id.iv_icon);
        vh.tv_name = view.findViewById(R.id.tv_name);
        vh.tv_content = view.findViewById(R.id.tv_content);
        vh.btn_xz = view.findViewById(R.id.btn_xz);
        vh.tv_index = view.findViewById(R.id.tv_index);
        return vh;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final AllDataEntry.ARuanjianLiebiaos obj= list.get(position);
        holder.tv_index.setText(obj.xzcs+"");
        Glide.with(context).load(UriApi.URI_API+obj.path)
                .apply(new RequestOptions().placeholder(R.mipmap.ic_launcher))
                .into(holder.iv_icon);
        holder.tv_name.setText(""+obj.name);
        holder.tv_content.setText(""+obj.text);
        holder.btn_xz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent= new Intent();
                intent.setAction("android.intent.action.VIEW");
                Uri content_url = Uri.parse(obj.url);
                intent.setData(content_url);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    public interface OnClick{
        void onClick(int position);
        void onDel(int position);
    }
    public void refresh(List<AllDataEntry.ARuanjianLiebiaos> _list) {
        list = _list;
        notifyDataSetChanged();
    }

    public void add(List<AllDataEntry.ARuanjianLiebiaos> _list) {
        list.addAll(_list);
        notifyDataSetChanged();
    }

}